#include "pantalla.h"

/*------------------------------------------------------------
	Constructor
------------------------------------------------------------*/
CPantalla::CPantalla()
{
	sPantalla=NULL;
	m_pFollowMe=NULL;
	m_pEscenario=NULL;
	m_CamX=0;
	m_CamY=0;
	m_CamZ=0;	
	
	roll=0;
}

/*------------------------------------------------------------
	Destructor
------------------------------------------------------------*/
CPantalla::~CPantalla()
{
}

/*------------------------------------------------------------
	Frees Memory
------------------------------------------------------------*/
void CPantalla::Liberar()
{	
	if(sPantalla){SDL_FreeSurface (sPantalla);}

	sPantalla=NULL;
//	m_Personaje=NULL;
	m_pFollowMe=NULL;
	m_pEscenario=NULL;
	
	SDL_Quit();
	printf("- Modulo Pantalla Finalizado \n");
}

/*------------------------------------------------------------
	Initialize the class
------------------------------------------------------------*/
Bool CPantalla::Inicializar (int _ancho,int _alto, int _pcompleta, char _nombre[63])
{	
	const SDL_VideoInfo *VideoInfo;
	int FlagsVideo;
	
	if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER))
	{
		printf ("ERROR - SDL_Init: %s\n", SDL_GetError ());
		return FALSE;
	}		
	
	
    VideoInfo = SDL_GetVideoInfo( );

    if ( !VideoInfo )
    {
	    printf("ERROR - GetVideoInfo: %s\n",SDL_GetError( ) );
	    return FALSE;
	}
	
	FlagsVideo  = SDL_OPENGL;          /* Enable OpenGL in SDL */
    FlagsVideo |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
    FlagsVideo |= SDL_HWPALETTE;       /* Store the palette in hardware */
    FlagsVideo |= SDL_RESIZABLE;       /* Enable window resizing */

	// Comprobamos si se puede usar la memoria para guardar surfaces
	if ( VideoInfo->hw_available )
		FlagsVideo |= SDL_HWSURFACE;
    else
		FlagsVideo |= SDL_SWSURFACE;
		
	// El blit puede hacerse por HW?
    if ( VideoInfo->blit_hw )
		FlagsVideo |= SDL_HWACCEL;


	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

	sPantalla = SDL_SetVideoMode (_ancho, _alto, 16, FlagsVideo);
	

	if (sPantalla == NULL)
	{
		printf ("ERROR - SetVideoMode: %s\n", SDL_GetError ());
		return FALSE;
	}	
		
	
	SDL_WM_SetCaption (_nombre, NULL);
	
	if(_pcompleta)
		SDL_WM_ToggleFullScreen (sPantalla);
	

    glEnable(GL_TEXTURE_2D);
    glShadeModel( GL_SMOOTH );    
    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );    
    glClearDepth( 1.0f );
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

	
	printf("+ Modulo Pantalla Inicializado\n");	

	return TRUE;
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void CPantalla::Dibujar()
{	
	
	if(m_pFollowMe)
		m_CamX=m_pFollowMe->getX();
	
	if(m_CamX<1)
		m_CamX=1;
	
	if(m_pEscenario && m_CamX>((m_pEscenario->getLargo())*LARGO_BLOQUE)-1)
		m_CamX=(m_pEscenario->getLargo()*LARGO_BLOQUE)-1;
		
		
//	m_CamY=m_Personaje->getY();
	
	
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluLookAt(	m_CamX, m_CamY, 0.0000001,
        		m_CamX, m_CamY, 0,
        		0.0f, 0.1f, 0.0f);
    glMatrixMode(GL_MODELVIEW);	
}

/*------------------------------------------------------------
	Updates the screen
------------------------------------------------------------*/
void CPantalla::Actualizar()
{
	SDL_GL_SwapBuffers();
}


